<?php

declare(strict_types=1);
/**
 * This file is part of bearing-web.
 *
 * @link     https://www.bearing.cn/
 * @contact  tech@bearing.cn
 * @license  https://gitee.com/cppo/bearing_web/-/blob/master/README.md
 */

namespace console\controllers;

use common\models\ProductCateModel;
use common\models\ProductModel;
use common\services\search\BaseSearchService;
use yii\base\InvalidConfigException;
use yii\elasticsearch\Exception;

class SearchController extends BaseController
{
    public function actionIndex(): void
    {
        $this->stdout('start...');
        try {
            $items = \Yii::$app->params['searchList'];
            foreach ($items as $item) {
                $model = new $item();
                if (! $model instanceof BaseSearchService || ! $model->isValid()) {
                    continue;
                }
                if (! $model->checkIndex()) {
                    $model->createIndex();
                }
                $this->stdout(basename(get_class($model)) . ' done...');
            }
        } catch (Exception|InvalidConfigException $e) {
            $this->stdout('fail:' . $e->getMessage());
        }
        $this->stdout('end...');
    }

    /**
     * @throws \yii\db\Exception
     */
    public function actionData(): void
    {
        $this->stdout('start...');
        try {
            $cls = \Yii::$app->params['searchList'];
            foreach ($cls as $classname) {
                $service = new $classname();
                if (! $service instanceof BaseSearchService || ! $service->isValid()) {
                    continue;
                }
                $lists = ProductCateModel::findAll(['search_doc' => $service->getSearchDocId()]);
                if (! $lists) {
                    continue;
                }

                foreach ($lists as $list) {
                    if (! $list instanceof ProductCateModel || ! $list->id) {
                        continue;
                    }

                    $this->stdout("start cate {$list->id} ...");
                    $productId = 0;
                    while (true) {
                        $objects = ProductModel::find()->andWhere([
                            'and',
                            ['>', 'id', $productId],
                            ['=', 'cate_id', $list->id],
                        ])->orderBy('id ASC')
                            ->limit(10)
                            ->all();
                        if (! $objects) {
                            break;
                        }
                        foreach ($objects as $object) {
                            if ($object instanceof ProductModel && $object->id) {
                                $this->stdout("start product {$object->id} ...");
                                if ($service->updateData($object)) {
                                    if (! $object->create_time) {
                                        $object->create_time = time();
                                    } else {
                                        $object->update_time = time();
                                    }
                                    $object->save();
                                }
                                $this->stdout("end product {$object->id} ...");
                                $productId = $object->id;
                            }
                        }
                    }
                    $this->stdout("end cate {$list->id} ...");
                }
            }
        } catch (Exception|InvalidConfigException $e) {
            $this->stdout('fail:' . $e->getMessage());
        }
        $this->stdout('end...');
    }
}
